package controllers

import (
	"github.com/astaxie/beego"
)

// ControllerError 错误码结构.
type ControllerError struct {
	Status   int    `json:"status"`
	Code     int    `json:"code"`
	Message  string `json:"message"`
	DevInfo  string `json:"dev_info"`
	MoreInfo string `json:"more_info"`
}

// 预定义错误码.401错误要求重新登录
var (
	Err404         = &ControllerError{404, 404, "page not found", "page not found", ""}
	ErrInputData   = &ControllerError{200, 10001, "数据输入错误", "客户端参数错误", ""}
	ErrDatabase    = &ControllerError{200, 10002, "服务器错误", "数据库操作错误", ""}
	ErrDupUser     = &ControllerError{200, 10003, "用户信息已存在或账号重复", "数据库记录重复", ""}
	ErrNoUser      = &ControllerError{401, 10004, "用户信息不存在", "数据库记录不存在", ""}
	ErrPass        = &ControllerError{401, 10005, "用户信息不存在或密码不正确", "密码不正确", ""}
	ErrNoUserPass  = &ControllerError{401, 10006, "用户信息不存在或密码不正确", "数据库记录不存在或密码不正确", ""}
	ErrToken       = &ControllerError{401, 10007, "用户认证错误", "token信息错误", ""}
	ErrInvalidUser = &ControllerError{401, 10008, "用户信息错误", "token信息认证错误", ""}
	ErrOpenFile    = &ControllerError{200, 10009, "服务器错误", "打开文件出错", ""}
	ErrWriteFile   = &ControllerError{200, 10010, "无法修改配置文件！", "写文件出错", ""}
	ErrSystem      = &ControllerError{200, 10011, "服务器错误", "操作系统错误", ""}
	ErrExpired     = &ControllerError{401, 10012, "登录已过期", "验证token过期", ""}
	ErrPermission  = &ControllerError{200, 10013, "没有操作权限", "没有操作权限", ""}
	ErrAes         = &ControllerError{200, 10013, "服务器错误", "AES加密解密错误", ""}
	ErrPid         = &ControllerError{200, 10014, "不允许删除有子对象的对象", "不允许删除有子对象的对象", ""}
	ErrDupChanel   = &ControllerError{200, 10015, "频道标识不允许重复！", "频道标识不允许重复！", ""}
	ErrNoData      = &ControllerError{202, 10016, "没有数据", "找不到记录", ""}
	ErrDisk        = &ControllerError{200, 10017, "存储空间已满", "存储空间已满", ""}
	ErrFds         = &ControllerError{200, 10018, "服务器错误", "文件服务器错误", ""}
	Actionsuccess  = &ControllerError{200, 0, "操作成功", "操作成功", ""}
)

// ErrorController 定义.
type ErrorController struct {
	CommonController
}

// RetError return error informatino in JSON.返回错误信息
func (c *ErrorController) RetError(e *ControllerError) {
	if mode := beego.AppConfig.String("runmode"); mode == "prod" {
		e.DevInfo = ""
	}
	c.Data["json"] = e
	c.ServeJSON()
}

// Error404 redefine 404 error information.404错误信息
func (c *ErrorController) Error404() {
	c.RetError(Err404)
}
